package com.ruoyi.project.system.record.service.impl;

import java.util.List;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.project.system.baseinfo.domain.TNewbornBaseinfo;
import com.ruoyi.project.system.baseinfo.service.ITNewbornBaseinfoService;
import com.ruoyi.project.system.record.domain.TDiseaseRecord;
import com.ruoyi.project.system.record.service.ITDiseaseRecordService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.project.system.record.mapper.TTreatmentRecordMapper;
import com.ruoyi.project.system.record.domain.TTreatmentRecord;
import com.ruoyi.project.system.record.service.ITTreatmentRecordService;
import com.ruoyi.common.utils.text.Convert;

/**
 * 疾病治疗记录Service业务层处理
 *
 * @author
 * @date 2021-03-31
 */
@Service
public class TTreatmentRecordServiceImpl implements ITTreatmentRecordService
{
    @Autowired
    private TTreatmentRecordMapper tTreatmentRecordMapper;

    @Autowired
    private ITDiseaseRecordService itDiseaseRecordService;

    @Autowired
    private ITNewbornBaseinfoService itNewbornBaseinfoService;

    /**
     * 查询疾病治疗记录
     *
     * @param id 疾病治疗记录ID
     * @return 疾病治疗记录
     */
    @Override
    public TTreatmentRecord selectTTreatmentRecordById(Long id)
    {
        return tTreatmentRecordMapper.selectTTreatmentRecordById(id);
    }

    /**
     * 查询疾病治疗记录列表
     *
     * @param tTreatmentRecord 疾病治疗记录
     * @return 疾病治疗记录
     */
    @Override
    public List<TTreatmentRecord> selectTTreatmentRecordList(TTreatmentRecord tTreatmentRecord)
    {
        return tTreatmentRecordMapper.selectTTreatmentRecordList(tTreatmentRecord);
    }

    /**
     * 新增疾病治疗记录
     *
     * @param tTreatmentRecord 疾病治疗记录
     * @return 结果
     */
    @Override
    public int insertTTreatmentRecord(TTreatmentRecord tTreatmentRecord)
    {
        tTreatmentRecord.setCreateTime(DateUtils.getNowDate());
        TDiseaseRecord tDiseaseRecord = itDiseaseRecordService.selectTDiseaseRecordById(tTreatmentRecord.getDiseaseId());
        if(tDiseaseRecord != null){
            tDiseaseRecord.setStatus(2);
            itDiseaseRecordService.updateTDiseaseRecord(tDiseaseRecord);
            TNewbornBaseinfo tNewbornBaseinfo = itNewbornBaseinfoService.selectTNewbornBaseinfoById(tDiseaseRecord.getNewbornId());
            tNewbornBaseinfo.setStatus(4);
            itNewbornBaseinfoService.updateTNewbornBaseinfo(tNewbornBaseinfo);
        }
        return tTreatmentRecordMapper.insertTTreatmentRecord(tTreatmentRecord);
    }

    /**
     * 修改疾病治疗记录
     *
     * @param tTreatmentRecord 疾病治疗记录
     * @return 结果
     */
    @Override
    public int updateTTreatmentRecord(TTreatmentRecord tTreatmentRecord)
    {
        tTreatmentRecord.setUpdateTime(DateUtils.getNowDate());
        return tTreatmentRecordMapper.updateTTreatmentRecord(tTreatmentRecord);
    }

    /**
     * 删除疾病治疗记录对象
     *
     * @param ids 需要删除的数据ID
     * @return 结果
     */
    @Override
    public int deleteTTreatmentRecordByIds(String ids)
    {
        return tTreatmentRecordMapper.deleteTTreatmentRecordByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除疾病治疗记录信息
     *
     * @param id 疾病治疗记录ID
     * @return 结果
     */
    @Override
    public int deleteTTreatmentRecordById(Long id)
    {
        return tTreatmentRecordMapper.deleteTTreatmentRecordById(id);
    }

    /**
     * 根据疾病ID查询当前治疗次数
     * @param id
     * @return
     */
    @Override
    public int selectCountByDiseaseId(Long id) {
        return tTreatmentRecordMapper.selectCountByDiseaseId(id);
    }
}
